#!/usr/bin/python
import pygame, sys, os
import Game, ResourceManager, EventHandler, Messages
from pygame.locals import *

class Main(object):

   WINDOW_SIZE = (800, 600)
   WINDOW_TITLE = 'Evil Greg VS. The 8 Year Olds'

   def __init__(self):
      pygame.init()
      pygame.mixer.init()

      #setup up window
      self.window = pygame.display.set_mode(self.WINDOW_SIZE)
      pygame.display.set_caption(self.WINDOW_TITLE)
      self.screen = pygame.display.get_surface()

      self.rManager = ResourceManager.ResourceManager()
      self.rManager.loadResources()

      self.highScore = 0

      self.game = Game.Game(self.screen, self.rManager)


   def go(self):
      while True:
         self.game.background.draw()
         Messages.printInstructions(self.screen)
         pygame.display.flip()
         pygame.mixer.music.load(os.path.join('sounds', 'intro_song.ogg'))
         pygame.mixer.music.set_volume(1.0)
         pygame.mixer.music.play(-1)
                                        
         EventHandler.waitForKeyOrQuit(K_RETURN)
         pygame.mixer.music.stop()
         pygame.mixer.music.load(os.path.join('sounds','game_song.ogg'))
         self.game.mainLoop()
         
         self.stopGame()
         self.game.reset()


   def stopGame(self):
      pygame.mixer.music.stop()
      pygame.mixer.music.load(os.path.join('sounds','dead_song.ogg'))
      pygame.mixer.music.play(0)
      Messages.printGameOver(self.screen)
      score = self.game.getScore()
      if score > self.highScore: self.highScore = score

      Messages.printScores(self.screen, score, self.highScore)

      pygame.display.flip()
      EventHandler.waitForKeyOrQuit(K_RETURN)



m = Main()

m.go()
